function Account(id, name, money) {
	this.id = id;
	this.name = name;
	this.money = money;
}
Account.prototype = {
    Add : function(){
        var url = "phps/AddAccount.php?";
        url = AddParam(url,"money",this.money);
        url = AddParam(url,"name",this.name);
        $.get(url);
    },
    Update : function(){
        var url = "phps/ModifyAccount.php?";
        url = AddParam(url,"id",this.id);
        url = AddParam(url,"money",this.money);
        url = AddParam(url,"name",this.name); 
        
        $.get(url);
    }
}
function Category(id, type, imgid, name) {
	this.id = id;
	this.type = type;
	this.imgid = imgid;
	this.name = name;
    this.money = 0;
}

function Turnover(id, money, remark, from, to, time, type, catid) {
	this.id = id;
	this.money = money;
	this.remark = remark;
	this.from = from;
	this.to = to;
	this.time = time;
	this.type = type;
	this.catid = catid;
}
Turnover.prototype = {
    Update : function(){
        var url = "phps/ModifyTurnover.php?";
        url = AddParam(url,"id",this.id);
        url = AddParam(url,"money",this.money);
        url = AddParam(url,"catid",this.catid); 
        url = AddParam(url,"from",this.from);
        url = AddParam(url,"to",this.to);
        url = AddParam(url,"time",this.time);
        url = AddParam(url,"remark",this.remark);
        
        
        
        $.get(url);
    },
    
    Add : function()
    {
        var url = "phps/AddTurnover.php?";
        url = AddParam(url,"money",this.money);
        url = AddParam(url,"catid",this.catid); 
        url = AddParam(url,"from",this.from);
        url = AddParam(url,"to",this.to);
        url = AddParam(url,"time",this.time);
        url = AddParam(url,"remark",this.remark);
        url = AddParam(url,"type",this.type);
        //console.log(url);
        //使用同步处理防止页面在还没请求完就跳转了
        jQuery.ajax({
            async : false,
            url : url,
            });
    }
}
function DataModel() {

}
DataModel.prototype = {
	accounts : new Array(),
    turnovers : new Array(),
    categorys : new Array(),

	init : function() {
	   
		var getdataurl = "phps/GetAllDatas.php";
        var self = this;
        
        jQuery.ajax({
            async : false,
            url : getdataurl,
            success : function(data,textStatus)
            {
            	self.accounts = new Array(),
               self.turnovers = new Array(),
                self.categorys = new Array(),
                //accounts
			$(data).find("account").each(function() {
				var id = $(this).find("id").text();
				var name = $(this).find("name").text();
				var money = $(this).find("money").text();
                
				var account = new Account(id, name, money);
				self.accounts.push(account);
			});
            //categorys
            $(data).find("category").each(function() {
                
				var id = $(this).find("id").text();
				var name = $(this).find("name").text();
				var type = $(this).find("type").text();
                var imgid = $(this).find("imgid").text();
                
				var category = new Category(id, type,imgid,name);
				self.categorys.push(category);
                
			});
            //turnovers
            $(data).find("turnover").each(function() {
                
				var id = $(this).find("id").text();
				var remark = $(this).find("remark").text();
				var money = $(this).find("money").text();
                var time = $(this).find("time").text();
                var type = $(this).find("type").text();
                var to = $(this).find("to").text();
                var from = $(this).find("from").text();
                var catid = $(this).find("catid").text();
                
                if(type == 0)
                {
                    for(var key in self.categorys)
                    {
                        if(self.categorys[key].id == catid)
                        {
                            var currentMoney = parseInt(self.categorys[key].money);
                            currentMoney = currentMoney + parseInt(money);
                            self.categorys[key].money = currentMoney;
                        }
                    }
                    
                }
                
                
				var turnover = new Turnover(id,money,remark,from,to,time,type,catid);
				self.turnovers.push(turnover);
                
			});

            }
            
        }
        );      
	},
    
       getCategory : function(id) {
        
        // var self = this;
         
        for(var key in this.categorys)
        {
            if(this.categorys[key].id == id)
            {
                return this.categorys[key];
            }
        }
        
    },
    getAccount : function(id) {
        
        // var self = this;
         
        for(var key in this.accounts)
        {
            if(this.accounts[key].id == id)
            {
                return this.accounts[key];
            }
        }
        
    },
    
    getMonthIncome : function(year,month){
        var totalIncomes = 0;
      for(var key in this.turnovers)
      {
        var turnover = this.turnovers[key];
        var currDate = StrToDate(turnover.time);
        if(year == currDate.getFullYear() && currDate.getMonth() + 1 == month && turnover.type == 1)
        {
            totalIncomes += parseFloat(turnover.money);
        }
        
      }  
      return totalIncomes;
    },
    
        getMonthOutgo : function(year,month){
          var totalOutgos = 0;
          for(var key in this.turnovers)
          {
            var turnover = this.turnovers[key];
            var currDate = StrToDate(turnover.time);
            if(year == currDate.getFullYear() && currDate.getMonth() + 1 == month && turnover.type == 0)
            {
                totalOutgos += parseFloat(turnover.money);
            }
            
          }  
          return totalOutgos;
    },
    //获得所有资产
    getTotalAssets : function()
    {
        var totalAsset = 0;
        for(var key in this.accounts)
        {
            var account = this.accounts[key];
            if(account.money > 0)
            {
                totalAsset += parseFloat(account.money);    
            }
             
        }
        return totalAsset;
    },
    getTotalDebts  : function()
    {
         var totalDebt = 0;
        for(var key in this.accounts)
        {
            var account = this.accounts[key];
            if(account.money < 0)
            {
                totalDebt += parseFloat(account.money);    
            }
             
        }
        return totalDebt;
    },
    

}
    var datamodel = new DataModel();